import * as nodeChildProcess from "child_process";
import {ChildProcess, ExecException, ExecOptions} from "child_process";

const childProcess = {
    ...nodeChildProcess,
    execPromise(cmd: string, option?: ExecOptions , print: boolean = true, callback?: ((err: ExecException | null, stdout: string | Buffer, stderr:string | Buffer) => void) | undefined) {
        return new Promise((resolve, reject) => {
            const cmdProcess = childProcess.exec(cmd, option, callback);
            if (print) {
                console.info(`cmd exec: [${cmd}]`)
            }
            cmdProcess.on("error", (err) => {
                reject(err);
            });
            cmdProcess.on("close", () => {
                resolve(cmdProcess);
            });
        });
    }
};

export { childProcess };
